package org.fusionide.builder.parsers.cfml;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;

import org.fusionide.builder.parsers.cfml.tags.Abort;
import org.fusionide.builder.parsers.cfml.tags.Argument;
import org.fusionide.builder.parsers.cfml.tags.Catch;
import org.fusionide.builder.parsers.cfml.tags.Component;
import org.fusionide.builder.parsers.cfml.tags.CustomTag;
import org.fusionide.builder.parsers.cfml.tags.Dump;
import org.fusionide.builder.parsers.cfml.tags.Else;
import org.fusionide.builder.parsers.cfml.tags.ElseIf;
import org.fusionide.builder.parsers.cfml.tags.Flush;
import org.fusionide.builder.parsers.cfml.tags.Function;
import org.fusionide.builder.parsers.cfml.tags.If;
import org.fusionide.builder.parsers.cfml.tags.Loop;
import org.fusionide.builder.parsers.cfml.tags.Output;
import org.fusionide.builder.parsers.cfml.tags.Query;
import org.fusionide.builder.parsers.cfml.tags.Return;
import org.fusionide.builder.parsers.cfml.tags.Set;
import org.fusionide.builder.parsers.cfml.tags.Try;


public class CFMLTagFactory {
	private static Hashtable<String, Class> tags = new Hashtable<String, Class>();

	static {
		tags.put("cfcomponent", Component.class);
		tags.put("cffunction", Function.class);
		tags.put("cfargument", Argument.class);
		tags.put("cfset", Set.class);
		tags.put("cfif", If.class);
		tags.put("cfelseif", ElseIf.class);
		tags.put("cfelse", Else.class);
		tags.put("cfdump", Dump.class);
		tags.put("cftry", Try.class);
		tags.put("cfcatch", Catch.class);
		tags.put("cfabort", Abort.class);
		tags.put("cfflush", Flush.class);
		tags.put("cfloop", Loop.class);
		tags.put("cfoutput", Output.class);
		tags.put("cfreturn", Return.class);
		tags.put("cf_", CustomTag.class);
		tags.put("cfquery", Query.class);
	}

	public static Class getTagClass(String tagName) {
		return tags.get(tagName);
	}

	public static CFMLTag createTag(String tagName, String attributes, int startOffset) {
		if (tagName.toLowerCase().startsWith("cf_"))
			tagName = "cf_";

		Class tagClass = tags.get(tagName.toLowerCase());
		if (tagClass == null)
			return null;
		CFMLTag tag = null;
		try {
			Constructor tagConstructor = tagClass.getConstructor(new Class[] { String.class, int.class });
			tag = (CFMLTag) tagConstructor.newInstance(new Object[] { attributes, new Integer(startOffset) });
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tag;
	}
}
